package com.teng.smartdrawer;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.teng.gallerylibrary.BaseAdapterHelper;
import com.teng.gallerylibrary.Gallery;
import com.teng.gallerylibrary.QuickPagerAdapter;
import com.teng.smartdrawer.utils.ImageLoaderTools;
import com.teng.smartdrawer.utils.PreferenceUtils;
import com.teng.smartdrawer.utils.ScreenUtils;
import com.teng.smartdrawer.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

public class TextMainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener, ViewPager.OnPageChangeListener, View.OnClickListener {
    private SwipeRefreshLayout mRefreshLayout;
    private List<String> imageUrls;
    private int headPosition;
    private SlidingMenu slidMenu;
    private Gallery gallery;
    private QuickPagerAdapter<Bean> mQuickPagerAdapter;
    private List<Bean> data;
    private LinearLayout search_linear , main_layout;
    private PreferenceUtils preferenceUtils;
    private ListView list_view;
    private TextView module_name , module_describe;
    private ImageView main_top_welcome_bg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenUtils.setTranslucentStatus(this);
        setContentView(R.layout.activity_text_main);



        preferenceUtils = PreferenceUtils.getInstance(this);
        //setSlidingOption();

        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//        NavigationView mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
        //setupDrawerContent(mNavigationView);


        ///////////////  上面的广告位  ///////////////
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.main_refreshlayout);
        mRefreshLayout.setColorSchemeResources(R.color.black , R.color.google_blue, R.color.google_yellow,R.color.google_blue, R.color.google_yellow);
        mRefreshLayout.setDistanceToTriggerSync(500);   // 下拉刷新的距离
        mRefreshLayout.setOnRefreshListener(this);

        imageUrls = new ArrayList<String>();
        imageUrls.add("http://www.bz55.com/uploads1/allimg/120312/1_120312100435_8.jpg");
        imageUrls.add("http://img3.iqilu.com/data/attachment/forum/201308/21/192654ai88zf6zaa60zddo.jpg");
        imageUrls.add("http://img2.pconline.com.cn/pconline/0706/19/1038447_34.jpg");

        main_top_welcome_bg = (ImageView) findViewById(R.id.main_top_welcome_bg);
        ImageLoader.getInstance().displayImage(imageUrls.get(0), main_top_welcome_bg);

        /////////////   下面的滑动浏览  ////////////////////
        module_describe = (TextView) findViewById(R.id.module_describe);
        module_name = (TextView) findViewById(R.id.module_name);

        search_linear = (LinearLayout) findViewById(R.id.main_fragment_linear_search);
        search_linear.setOnClickListener(this);

        ImageView main_menu = (ImageView) findViewById(R.id.img_main_fragment_sliding);
        main_menu.setOnClickListener(this);

        data = new ArrayList<Bean>();
//        data.add(new Bean(R.drawable.a, R.string.describe_string_a , R.string.demo_string_a));
        data.add(new Bean(R.drawable.b, R.string.describe_string_b ,R.string.demo_string_b));
        data.add(new Bean(R.drawable.c, R.string.describe_string_c ,R.string.demo_string_c));
        data.add(new Bean(R.drawable.d, R.string.describe_string_d ,R.string.demo_string_d));
        data.add(new Bean(R.drawable.e, R.string.describe_string_e ,R.string.demo_string_e));
//        data.add(new Bean(R.drawable.f, R.string.describe_string_f ,R.string.demo_string_f));
//        data.add(new Bean(R.drawable.g, R.string.describe_string_g ,R.string.demo_string_g));
//        data.add(new Bean(R.drawable.h, R.string.describe_string_h ,R.string.demo_string_h));
//        data.add(new Bean(R.drawable.i, R.string.describe_string_i,R.string.demo_string_i));
//        data.add(new Bean(R.drawable.j, R.string.describe_string_j ,R.string.demo_string_j));
//        data.add(new Bean(R.drawable.k, R.string.describe_string_k ,R.string.demo_string_k));

        mQuickPagerAdapter = new QuickPagerAdapter<Bean>(this, R.layout.gallery_item_layout, data) {
            @Override
            protected void convertView(BaseAdapterHelper helper, final Bean item) {
                helper.setImageResource(R.id.imageview, item.getImgResId());
                helper.setImageOnClickListener(R.id.imageview, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
        };

        gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(mQuickPagerAdapter);

        /////////////////   ViewPager的一些操作  ///////////////////////
        gallery.getViewPager().addOnPageChangeListener(this);
        gallery.getViewPager().setCurrentItem(1);
        gallery.getViewPager().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO 解决ViewPager和SwipeRefreshLayout的滑动冲突
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        mRefreshLayout.setEnabled(false);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mRefreshLayout.setEnabled(true);
                        break;
                }
                return false;
            }
        });

    }


    @Override
    public void onRefresh() {
        headPosition++;
        if (headPosition == imageUrls.size()){
            headPosition = 0;
        }

        ImageLoader.getInstance().loadImage(imageUrls.get(headPosition), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                main_top_welcome_bg.setImageBitmap(bitmap);
                mRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_main_fragment_sliding :
                // TODO 侧滑菜单
                if (!slidMenu.isMenuShowing()) {
                    slidMenu.showMenu();//显示左菜单（主菜单）
                } else {
                    slidMenu.toggle();
                }
                break;
            case R.id.main_fragment_linear_search :
                // TODO 搜索
                Intent toSearch = new Intent(this , SearchActivity.class);
                startActivity(toSearch);
                break;
            case R.id.component_find:
                // TODO 零件查找
                Intent toComponentSearch = new Intent(this , SearchComponentActivity.class);
                startActivity(toComponentSearch);
                break;
            case R.id.module_find:
                // TODO 模块查找
                Intent toModuleSearch = new Intent(this , SearchModuleActivity.class);
                startActivity(toModuleSearch);
                break;
            case R.id.buy_component:
                //TODO 购买

                break;
            case R.id.login_linear:
                //TODO 登录
                Intent goMainIntent = new Intent(this , LoginActivity.class);
                startActivity(goMainIntent);
                break;
            case R.id.about_app:
                //TODO 关于
                Intent aboutMainIntent = new Intent(this , AboutActivity.class);
                startActivity(aboutMainIntent);
                break;
            default:
                break;
        }
    }

    private void setSlidingOption(){

        slidMenu = new SlidingMenu(this);
        slidMenu.setMode(SlidingMenu.SLIDING_WINDOW);//左菜单
        slidMenu.setMenu(R.layout.slideing_layout);
        //设置菜单侧滑出来的触摸方式，不能手动出
        slidMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        // 设置渐入渐出效果的值
        slidMenu.setFadeDegree(0.35f);
        // 设置菜单侧滑出来的宽度(屏幕的3/4)
        slidMenu.setBehindWidth((int)
                (getResources().getDisplayMetrics().widthPixels * 0.75f));//单位：px
        //slidMenu.setBehindOffset(300);//设置菜单侧滑出来后，内容页面显示的宽度
        //设置菜单的归属模式
        slidMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

        View menu = slidMenu.getMenu();
        /////////////////    侧滑菜单    //////////////////
        LinearLayout buy_component = (LinearLayout) menu.findViewById(R.id.buy_component);
        buy_component.setOnClickListener(this);

        LinearLayout component_find = (LinearLayout) menu.findViewById(R.id.component_find);
        component_find.setOnClickListener(this);

        LinearLayout module_find = (LinearLayout) menu.findViewById(R.id.module_find);
        module_find.setOnClickListener(this);

        LinearLayout login_linear = (LinearLayout) menu.findViewById(R.id.login_linear);
        login_linear.setOnClickListener(this);

        TextView user_name = (TextView) menu.findViewById(R.id.user_name);
        CircleImageView user_head = (CircleImageView) menu.findViewById(R.id.user_head);

        // 是否登录
        //MyApplication.isLogin;
        if (true){
            user_name.setText(preferenceUtils.getString(PreferenceUtils.USER_NAME , "小哥爱学习"));
            String pic_url = preferenceUtils.getString(PreferenceUtils.USER_HEAD_PIC, "");
            pic_url = "http://image.16888.com/picLib/57490/118165/2010122716392442_thumb800_600.jpg";
            ImageLoaderTools.getInstance(this).displayImage(pic_url, user_head);
        }else {
            user_name.setText("登录");
        }

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Bean bean = data.get(position);
        module_describe.setText(bean.getStrResId());
        module_name.setText(bean.getNameResId());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
